package cn.itsource.xhy.user.service.impl;

import cn.itsource.xhy.basic.service.impl.BaseServiceImpl;
import cn.itsource.xhy.user.contants.UserContants;
import cn.itsource.xhy.user.domain.LoginInfo;
import cn.itsource.xhy.user.domain.User;
import cn.itsource.xhy.user.domain.dto.UserDto;
import cn.itsource.xhy.user.mapper.LoginInfoMapper;
import cn.itsource.xhy.user.mapper.UserMapper;
import cn.itsource.xhy.user.service.IUserService;
import cn.itsource.xhy.utils.MD5Utils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.UUID;


@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private LoginInfoMapper loginInfoMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public LoginInfo login(User user) {
        // 验证用户名是否正确
        LoginInfo loginInfo = loginInfoMapper.login(user.getUsername());
        if (loginInfo == null) {
            throw new RuntimeException("1用户名或密码错误");
        }
        // 验证密码 输入的密码加盐加密
        String md5 = MD5Utils.encrypByMd5(user.getPassword() + loginInfo.getSalt());

        if (!md5.equals(loginInfo.getPassword())){
            throw new RuntimeException("2用户名或密码错误");
        }
        return loginInfo;
    }

    @Override
    public User findByPhone(String phone) {
        return userMapper.findByPhone(phone);
    }

    @Override
    public void register(UserDto userDto) {
        // 验证码是否正确
        String codeInRedis = (String)redisTemplate.opsForValue().get(UserContants.REGISTER_CODE+":"+userDto.getPhone());
        // 分割拿到验证码
        String code = codeInRedis.split(":")[0];
        // 判断与发送的是否一直
        if (!code.equals(userDto.getCode())){
            throw new RuntimeException("验证码有误，请重新输入");
        }
        // 调用方法将对象装进去
        User user = userDtoToUser(userDto);
        // 保存对象
        userMapper.save(user);
        // 将user对象转换为LonginInfo对象
        LoginInfo loginInfo = userToLoginInfo(user);
        // 保存对象
        loginInfoMapper.save(loginInfo);

    }


    // 将user对象转换为LonginInfo对象
    private LoginInfo userToLoginInfo(User user){
        LoginInfo loginInfo = new LoginInfo();
        // gjl复制数据
        BeanUtils.copyProperties(user,loginInfo);
        // 其他数据赋值
        loginInfo.setDisable(1);
        loginInfo.setType(1);
        loginInfo.setUId(user.getId());
        return loginInfo;
    }

    // 将临时对象的数据装进user中
    private User userDtoToUser(UserDto userDto){
        User user = new User();
        user.setPhone(userDto.getPhone());
        // 给用户密码生成盐值
        String salt = UUID.randomUUID().toString();
        // 工具类密码加密
        String str = MD5Utils.encrypByMd5(userDto.getPassword() + salt);
        user.setPassword(str);
        // 保存盐值
        user.setSalt(salt);
        // 手机注册，将状态默认开启
        user.setState(1);
        // 将对象返回
        return user;
    }
}


